home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c-part1 / 6032 < prev    next >
Encoding:
Text File  |  1996-08-05  |  3.7 KB  |  110 lines

  1. Path: keats.ugrad.cs.ubc.ca!not-for-mail
  2. From: c2a192@ugrad.cs.ubc.ca (Kazimir Kylheku)
  3. Newsgroups: comp.lang.c
  4. Subject: Re: More Modulus questions
  5. Date: 21 Feb 1996 14:02:04 -0800
  6. Organization: Computer Science, University of B.C., Vancouver, B.C., Canada
  7. Message-ID: <4gg4osINN9ku@keats.ugrad.cs.ubc.ca>
  8. References: <Pine.SOL.3.90.960219171637.21117B-100000@eddie> <4gfnka$ni7@spanky.pls.ov.com> <4gfp5a$r8e@cloner2.ix.netcom.com>
  9. NNTP-Posting-Host: keats.ugrad.cs.ubc.ca
  10.  
  11. In article <4gfp5a$r8e@cloner2.ix.netcom.com>, KPN  <wzjn@ix.netcom.com> wrote:
  12.  >Good Afternoon,
  13.  >
  14.  >Recently I asked a question concerning the proper use of the Modulus
  15.  >code. Many replied back to me to help clear up m confusion, and many
  16.  >replied back to the group to help me. I thank all of you for your time
  17.  >and expertise.
  18.  >
  19.  >As I looked over what had been sent to me, a great deal of the
  20.  >responses helped by pointing out that what I was doing was incorrect -
  21.  >however, no one really showed me what I should be doing to correct the
  22.  >problem! Well, actually, a few examples were to be had, but were so far
  23.  >over my knowledge of C thus far, as to be useless to me. Thank you
  24.  >anyway to those who sent in.
  25.  >
  26.  >Here again is my problem: (From æHow to Program CÆ second edition
  27.  >Deitel/Dietel - Write a program that reads an integer and determines,
  28.  >and prints how many digits in the integer are 7Æs)
  29.  > 
  30.  >#include <stdio.h>
  31.  >
  32.  >main()
  33.  >{
  34.  >  int number;
  35.  >  int first, second, third, fourth;
  36.  >  int integer1, integer2, integer3, integer4; 
  37.  >
  38.  >  printf("Enter a four digit number: ");
  39.  
  40. Should say "Enter a _non_negative_ four digit number".
  41. You should be using unsigned math. The modulus operator has
  42. implementation-defined results for negative operands.
  43.  
  44.  >  scanf("%d", &number);
  45.  
  46. Has to be fixed to %u once you convert to unsigned.
  47.  
  48.  >/*
  49.  >   Split the four digit number into four seperate integers
  50.  >*/
  51.  >
  52.  >  first = number / 1000;
  53.  >  integer1 = number % 1000;
  54.  
  55. why not use an accumulator?
  56.                     /* number is 0-9999 */
  57.     num[0] = number / 1000;        /* therefore num[0] is 0-9 */
  58.     residue = number % 1000;    /* residue is 0-999 */
  59.  
  60.     num[1] = residue / 100;        /* therefore num[1] is 0-9 */
  61.     residue %= 100;            /* residue is 0-99 */
  62.  
  63.     num[2] = residue / 100;        /* therefore num[2] is 0-9 */
  64.  
  65.     num[3] = residue % 10;        /* and num[3] is 0-9    */
  66.     
  67.  >  second = integer1 / 100;
  68.  >  integer2 = integer1 % 100;
  69.  >
  70.  >  third = integer2 / 10;
  71.  >  integer3 = integer2 % 10;
  72.  >
  73.  >  fourth = integer3 / 1;
  74.  >  integer4 = integer3 % 1;
  75.  
  76. Dividing by 1 is a waste. The remainder of anything divided by 1 is zero.
  77. Think: how could there be a residue in integer4?  You have completely
  78. determined all four digits of the integer which uniquely identify it. There
  79. can't be anything left over. See my above code.
  80.  
  81.  >/*
  82.  >   Use modulas to determine if any integers are a 7
  83.  >*/
  84.  >      
  85.  >   if (first % 7)
  86.  >      printf("First integer was a 7\n");
  87.  >   else
  88.  >      printf("Not a 7\n");
  89.  
  90. This is silly. You obviously did NOT read the replies to your posting.
  91. The four integers are guaranteed to be in the range 0-9 (assuming that a
  92. positive integer was input). Are you completely oblivious to the existence of
  93. the '==' operator? Why are you using a relatively expensive modulus operation
  94. in place of a comparison?  Maybe you should focus on mastering the use of
  95. frequently used operators.
  96.  
  97.     if (first == 7)
  98.         printf("First integer is a 7\n");
  99.  
  100.     OR
  101.  
  102.     puts((first == 7) ? "First digit is a 7\n" : "First digit is not 7\n");
  103.  
  104. Your use of '%' is wrong here even as a test test for numbers that are
  105. divisible by 7.  If "first" is actually equal to 7, then   the expression
  106. "first % 7" evaluates to zero, since 7 divides itself evenly. Hence your
  107. message will falsely report that the integer is _NOT_ 7.
  108. -- 
  109.  
  110.